package svc;

import java.util.ArrayList;

import constants.Constants;
import dao.FileDao;
import domain.file.FileDetails;

public class FileSvc {
	FileDao dao = new FileDao();
	
	/**
	 * Takes in userId
	 * returns List of Files Created by the user
	 */
	public ArrayList<FileDetails> getUserFileList(int userId) {
		return dao.getFileList(userId);
	}
	
	/**
	 * Takes in userId
	 * returns List of Files Created by the user and includes Default File
	 */
	public ArrayList<FileDetails> getAllFileList(int userId) {
		ArrayList<FileDetails> result = new ArrayList<FileDetails>();
		
		FileDetails defaultFile = dao.getDefaultFile();
		ArrayList<FileDetails> files = dao.getFileList(userId);
		
		boolean defaultExists = false;
		for(int i=0; i<files.size(); i++){
			if(files.get(i).getFileNo()==defaultFile.getFileNo()){
				defaultExists = true;
				break;
			}
		}
		
		if(!defaultExists){
			result.add(defaultFile);
		}
		result.addAll(files);
		
		return result;
	}
	
	/**
	 * Takes in userId
	 * returns List of All Files by User (including Default file) which are already Analyzed
	 */
	public ArrayList<FileDetails> getAnalyzedFileList(int userId) {
		ArrayList<FileDetails> result = new ArrayList<FileDetails>();
		
		ArrayList<FileDetails> files = this.getAllFileList(userId);
		
		for(int i=0; i<files.size(); i++){
			if(files.get(i).getAnalyzed() == Constants.ANALYZED){
				result.add(files.get(i));
			}
		}
		
		return result;
	}
	
	/**
	 * Takes in fileNo
	 * return true of file deleted successfully or if file does not exists
	 * return false if not able to delete
	 */
	public boolean deleteFile(int fileNo){
		return dao.deleteFile(fileNo);
	}
	
	
	/**
	 * Takes in New File Details
	 * defaultFileNo should be -1 in case of creating a New File and should be the "Base File No" in case of copying File
	 * return newly generated file no if file created successfully
	 * return -1 otherwise
	 */
	public int createNewFile(int userId, String fileName, String fileDesc, int defaultFileNo){
		return dao.createNewFile(userId, fileName, fileDesc, defaultFileNo);
	}
	
	public FileDetails getDefaultFile(){
		return dao.getDefaultFile();
	}
}